package com.xch.controller;

import com.xch.bean.Courier;
import com.xch.bean.Message;
import com.xch.bean.ResultData;
import com.xch.bean.User;
import com.xch.mvc.ResponseBody;
import com.xch.service.CourierService;
import com.xch.service.UserService;
import com.xch.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class UserController {
    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<User> list = UserService.findAll(true,offset,pageNumber);
        ResultData<User> data = new ResultData<>();
        int total = UserService.total();
        data.setRows(list);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println(json);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        String nickName = request.getParameter("name");
        String userPhone = request.getParameter("phone");
        String password = request.getParameter("password");

        User u = new User(nickName,userPhone,password);

        boolean flag = UserService.insert(u);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("用户录入成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("用户录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String phone = request.getParameter("phone");
        User u = UserService.findByPhone(phone);
        Message msg = new Message();
        if(u == null){
            msg.setStatus(-1);
            msg.setResult("号码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("已查找到");
            msg.setData(u);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println(json);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String nickName = request.getParameter("name");
        String userPhone = request.getParameter("phone");
        String password = request.getParameter("password");
        User u = new User(nickName,userPhone,password);
        boolean flag = UserService.update(id, u);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = UserService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("刪除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("刪除失敗");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
